package logic

import (
	"codejy/calculate"
	"codejy/globalfunc"
	"strconv"
)

type AndArithmeticCalculate struct {
	name          string                  //计算器名称
	priority      int                     //优先级
	calculateData calculate.CalculateData //计算器数据
	result        string                  //计算结果

}

func init() {
	//注册计算器
	calculate.RegisterCalculate(&AndArithmeticCalculate{
		name:     "&&",
		priority: 5,
	})
}

func (a *AndArithmeticCalculate) GetName() string {
	return a.name
}

func (a *AndArithmeticCalculate) IsBool(symbol string) bool {
	if symbol == a.name {
		// 判断是布尔值
		b1 := globalfunc.ExecuteFunc("IsBool", a.calculateData.A).(bool)
		b2 := globalfunc.ExecuteFunc("IsBool", a.calculateData.B).(bool)
		if b1 && b2 {
			return true
		}
		panic("逻辑运算  &&(并且) ,必须是布尔类型: " + a.calculateData.A + "&&" + a.calculateData.B)
	}
	return false
}

func (a *AndArithmeticCalculate) AddExpression(a1, symbol, b1 string) {
	a.calculateData = calculate.CalculateData{
		A:      a1,
		Symbol: symbol,
		B:      b1,
	}
}

func (a *AndArithmeticCalculate) Calculate() {
	a.result = strconv.FormatBool(a.calculateData.A == "true" && a.calculateData.B == "true")
}

func (a *AndArithmeticCalculate) GetResult() string {
	return a.result
}

func (a *AndArithmeticCalculate) GetPriority() int {
	return a.priority
}
